
//存储生成的表id名称[全部]（重要）
var bzcjCxFzZhuGuanTablesId=[]
//存储生成的表id名称[产线主管]（用于总计划主管）
var bzcjCxhuGuanTablesId=[];


//页面需要的初始化
$("#bzZhuGuanTatalMoneyInput").textbox("setValue",0);  //用户若做了一半又跳转另一个页面，若又转回就会有残留之前的数据

//基本字段
var  bzZgColumnsDefalt={
    basic:function () {
        return[
            {"field": 'id', "title": 'ID', "width": "5%", "align": "center", "hidden": "true"},
            {"field": 'empName', "title": '姓名', "width": "5%", "align": "center","editor": {"type": "text"}},
            {"field": 'empCode', "title": '工号', "width": "5%", "align": "center","editor": {"type": "text"}},
            {"field": 'keZhangName', "title": '所属科长', "width": "5%", "align": "center","editor": {"type": "text"},"hidden": "true"},
            {"field": 'postName', "title": '岗位名称', "width": "5%", "align": "center","editor": {"type": "text"}},
            {"field": 'performanceBonus', "title": '绩效<br/>奖金标准', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangZhuGuan}}},
            {"field": 'attendanceRate', "title": '出勤率<br/>(%)', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangZhuGuan}}},
            {"field": 'exceptionBonus', "title": '发现异常<br/>奖金', "width": "5%","align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangZhuGuan}}},
            {"field": 'illegalFine', "title": '违规扣款', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangZhuGuan}}},
            {"field": 'resultMoney', "title": '综合<br/>绩效（元）', "width": "5%","align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
            {"field": 'remarks', "title": '备注', "width": "21%","align": "center","editor": {"type":"textarea"}},
        ]
    },
    basic2:function () {
       var v=[
           {"field": 'penalty5s', "title": '规范化<br/>奖金', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangZhuGuan}}},
        {"field": 'score5s', "title": '5s得分', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":bzZhuGuanPenalty5s}}},
          ]
        var basic = bzZgColumnsDefalt.basic();
        v.forEach(function (val) {
            basic.splice(6,0,val)
        })
        return basic;
    }
}




//生成表字段，并初始化表
function bzCxFzZhuGuanTableInit(bzCxZgColumnsAndDataInit){
    // var bzCxZgColumnsInit=[
    //     {
    //         "data":[ {empName: "方水珠",empCode:"0056825",postName:"产线主管",name1:"800",name2:"800",name3:"800",name4:"800",name5:"800",
    //             performanceBonus:"",attendanceRate:"",exceptionBonus:"",illegalFine:"",resultMoney:"",remarks:"",assessType:"" }],
    //         "columns":[
    //             {"field": 'name1', "title": '李松娥', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":bzCxZhuGuanPerformanceBonus}}},
    //             {"field": 'name7', "title": '丘红云', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":bzCxZhuGuanPerformanceBonus}}}
    //         ]
    //     },
    //     {
    //         "data":[ {empName: "李松娥",empCode:"0056825",postName:"产线主管",name1:"800",name2:"800",name3:"800",name4:"800",name5:"800",
    //             performanceBonus:"",attendanceRate:"",exceptionBonus:"",illegalFine:"",resultMoney:"",remarks:"",assessType:"" }],
    //         "columns":[
    //             {"field": 'name1', "title": '蓝碧媚', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":bzCxZhuGuanPerformanceBonus}}},
    //             {"field": 'name6', "title": '吴伟花', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":bzCxZhuGuanPerformanceBonus}}},
    //             {"field": 'name7', "title": '孙勤勤', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":bzCxZhuGuanPerformanceBonus}}}]
    //     }
    // ]
    //获取组长字段和该字段的值:参数：年、月、部门（查员工表）、岗位名称、组长id+辅助组长id
    //把"onChange":bzCxZhuGuanPerformanceBonus中的bzCxZhuGuanPerformanceBonus字符串转换为fun类型
    bzCxZgColumnsAndDataInit.forEach(function (val) {
        var columns = val.columns;
        if (columns){
            columns.forEach(function (v) {
                var editor = v.editor;
                if (editor){
                    var options = editor.options;
                    var onChange = options.onChange;
                    if (onChange){
                        options.onChange= window[onChange]
                    }
                }
            })
        }
    })

    //生成表字段，并初始化表
    bzCxZgColumnsAndDataInit.forEach(function (val, index) {
        //获取组长的字段
        var columns = val.columns;
        //获取数据
        var data=val.data;

        //生成每张表的id，并存储到数组中
        let tableId="bzcjCxZhuGuanTable_"+index
        bzcjCxFzZhuGuanTablesId.push(tableId)

        //处理数据库获取的字段添加到数组中
        var postName = data[0].postName;// 获取岗位名称：
        var temp;
        //判断是辅助主管还是产线主管
        if (postName==JX_BZCJ_ZHUGUAN){  //产线主管
            temp= bzZgColumnsDefalt.basic()
            bzcjCxhuGuanTablesId.push(tableId)
        }else{
            temp=bzZgColumnsDefalt.basic2()
        }

        //往html中创建html标签table元素
        $("#bzcjCxZhuGuanTableDiv").append(bzcjCxZhuGuanTableHtml(tableId))

        //把数据库的组长数据添加到表中
        columns.forEach(function (v){
            temp.splice(5,0,v)
        })
        let resultColumns=[];
        resultColumns.push(temp)
        //最终生成的字段
        console.log(postName+"字段【"+index+"】",resultColumns);
        console.log(postName+"数据【"+index+"】",data);
        bzcjCxZhuGuanTableInit(tableId,resultColumns,data);
    })

}

//辅助主管和产线主管初始化表
function bzcjCxZhuGuanTableInit(tableId,columns,data){
    $("#"+tableId).edatagrid({
        nowrap: false,
        columns: columns,
        data:data,
        showFooter: true,
        autoSave:true,
        fitColumns: true,
        toolbar:"#"+tableId+"Toolbar",
        onAfterEdit:function (index, row) {
            //赋值：整个页面的总计金额
            totalBzSumMoney();
        },
        onEdit:function () {
            bzCxZhuGuanPerformanceBonus()
        }
    })
}

//html内容
function bzcjCxZhuGuanTableHtml(tableId) {
    let aa="javaScript:$(\"#"+tableId+"\").edatagrid('addRow')";
    let bb="javaScript:$(\"#"+tableId+"\").edatagrid('destroyRow')";
    let cc="javaScript:$(\"#"+tableId+"\").edatagrid('saveRow')";

    return  '<table id='+tableId+'></table>'+
        '<div id='+tableId+'Toolbar style="display: none;">\n' +
        '        <div style="width: 350px;">\n' +
        '            <a class="easyui-linkbutton c1" style="height: 90%" data-options="plain:true" onClick='+cc+'><i class="fa fa-floppy-o fa-fw" aria-hidden="true"></i>保存</a>\n' +
        // '            <a class="easyui-linkbutton" data-options="plain:true">|</a>\n' +
        // '            <a class="easyui-linkbutton c5" style="height: 90%" data-options="plain:true" onclick='+bb+'><i class="fa fa-trash fa-fw" aria-hidden="true"></i>删除</a>\n' +
        '        </div>\n' +
        // '      <div style="width: 250px;float:right;text-align: right;padding-right: 10px">\n'+
        // '           <a class="easyui-linkbutton" data-options="plain:true" onclick="'+bb+'"><i class="fa fa-floppy-o fa-fw" aria-hidden="true"></i>保存</a>\n' +
        // '        </div>\n' +
        // '      <!--清除浮动-->\n' +
        // '       <div style="clear: both"></div>\n' +
        '    </div>'
}

//获取本页面当前编辑行指定的jquery对象
function getEditBzcjZhuGuan(fieldValue) {
    let j;
    for (let v of bzcjCxFzZhuGuanTablesId){  //bzcjCxZhuGuanTablesId本页面的所有表的id
        try {
            j = $('#'+v).datagrid('getEditor', {
                index: 0,   //就用0索引，主管就一个字段
                field: fieldValue
            }).target;
            return j;
        }catch (e) {
        }
    }
    //若没有找到，去找总计划主管的
    if (j){
        return j
    }else {
       j= $('#bzcjTotalZhuGuanTable').datagrid('getEditor', {index: 0, field: fieldValue});
       if (j){
           return j.target;
       }
    }
}
//----------------------------------------------------------------------总计划主管开始

bzcjTotalZhuGuanTableInit()
function bzcjTotalZhuGuanTableInit(columns,data){
    $("#bzcjTotalZhuGuanTable").edatagrid({
        nowrap: false,
        columns: columns?columns:[bzZgColumnsDefalt.basic2()],
        data:data,
        showFooter: true,
        autoSave:true,
        fitColumns: true,
        toolbar:"#bzcjTotalZhuGuanTableToolbar",
        onBeforeEdit:function (index, row) { //编辑器打开触发事件
            bzcjTotalZhuGuanTableTableIndex=index;//保存当前索引
        },
        onAfterEdit:function (index, row) {
          //赋值：整个页面的总计金额
            totalBzSumMoney();
        },
        onDestroy:function (index,row) { //删除一行触发事件
            // loadBzZhuGuanFooter(tableId);
            totalBzSumMoney();
        }
    })
}

//加载数据
function loadBzcjTotalZhuGuanTableData(){
    var resultColumns=[];                                  //最终表字段
    var ini = bzZgColumnsDefalt.basic2();                   //获取最初字段
   // var data=[{empName: "苏惠芳",empCode:"0056825",postName:"总计划主管"}]   //最初数据
    var data=commonAjax("post","/jxEmp/getZhuGuanInfo",{postId:JX_BZCJ_ZONGJIHUAZHUGUAN_CODE})
    var j=0;//组长字段名字后尾----人数
    var sumResultMoney=0;  //产线主管的总和
    bzcjCxhuGuanTablesId.forEach(function (value) {
       var row= $("#"+value).datagrid("getRows");
       var empName = row[0].empName;
       var resultMoney = row[0].resultMoney;
        j++;
       if (resultMoney){
           sumResultMoney=Number(sumResultMoney)+Number(resultMoney);
       }
       var val='value'+j;   //为字段名称赋值
       var name='name'+j; //为数据中的名称赋值
       ini.splice(5,0,{"field": val, "title": empName, "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":bzCxZhuGuanPerformanceBonus}}});
       data[0][val]=resultMoney;
       data[0][name]=empName;

    })
    //总计划主管绩效奖金标准：全部产线主管的平均值
    sumResultMoney=fomatFloat(sumResultMoney/j,2);
    data[0].performanceBonus=sumResultMoney;
    data[0].resultMoney=sumResultMoney;
    resultColumns.push(ini)
    bzcjTotalZhuGuanTableInit(resultColumns,data)
}